package parser;


class PythonString {
    
    StringBuffer buf = new StringBuffer();
    
    public PythonString() {
    }
    
    public PythonString(String s) {
        append(s);
    }
    
    public void append(String s) {
        String text;
        boolean raw = false;
        if (s.charAt(0) == 'r') {
            raw = true;
            s = s.substring(1);
        }
        if (s.charAt(0) == '\'') {
            text = s.substring(1, s.length()-1);
        } else if (s.charAt(0) == '\"') { 
            if (s.length() > 5 && s.charAt(1) == '\"' && s.charAt(2) == '\"') {
                text = s.substring(3, s.length()-3);
            } else {
                text = s.substring(1, s.length()-1);
            }
        } else {
            throw new Error("Expecting a quoted string, got " + s);   
        }
        // Need to remove \ from strings:
        if (raw || text.indexOf('\\') < 0) {
            buf.append(text);
        } else {
            text = text.replace("\\n", "\n");   
            text = text.replace("\\t", "\t");   
            text = text.replace("\\\\", "\\"); 
            buf.append(text);
        }
    }
    
    public String toString() {
        return buf.toString();   
    }

}
